<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input v-model="queryInfo.query" placeholder="请输入内容" @clear="searchKey" clearable>
            <el-button slot="append" type="primary" icon="el-icon-search" @click="searchKey"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table :data="getGoodTable" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="80">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status==='1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
        <el-table-column label="下单时间" width="180">
          <template slot-scope="scope">{{scope.row.update_time|formatTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editAddressDialogVisible=true"
            ></el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="showKuaiDi"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改地址弹出层 -->
    <el-dialog title="修改地址" :visible.sync="editAddressDialogVisible" width="50%">
      <el-form
        :model="addressRuleForm"
        :rules="addressrules"
        ref="addressRuleFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressSelect"
            :options="citydata"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressRuleForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看物流弹出层 -->
    <el-dialog title="查看物流进度" :visible.sync="lookDialogVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(item,i) of kuaiDiList"
          :key="i"
          :timestamp="item.time"
        >{{item.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// 城市数据
import citydata from "../../assets/citydata";

export default {
  name: "orders",
  data() {
    return {
      queryInfo: {
        //查询参数
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      getGoodTable: [], //订单列表数据
      total: 0, //订单列表总数据
      editAddressDialogVisible: false, //修改地址弹框显示隐藏
      addressRuleForm: {
        //修改地址表单项
        address1: [],
        address2: ""
      },
      addressrules: {
        //修改地址表单校验规则
        address1: [{ required: true, message: "请选择城市", trigger: "blur" }],
        address2: [
          { required: true, message: "请填写详细的物流地址", trigger: "blur" }
        ]
      },
      citydata, //城市数据
      addressSelect: [], //城市选择数组
      lookDialogVisible: false, //查看物流进度弹框显示隐藏
      kuaiDiList: [] //物流数据
    };
  },
  methods: {
    searchKey() {
      //搜索
      this.getGoodData();
    },
    async getGoodData() {
      //获取订单列表数据
      const { data: res } = await this.$axios.get("orders", {
        params: {
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize
        }
      });
      if (res.meta.status === 200) {
        this.getGoodTable = res.data.goods;
        this.total = res.data.total;
      }
      console.log(res);
    },
    handleSizeChange(newSize) {
      //表格显示条数发生变化
      this.queryInfo.pagesize = newSize;
      this.getGoodData();
    },
    handleCurrentChange(newPage) {
      //表格跳转发生变化
      this.queryInfo.pagenum = newPage;
      this.getGoodData();
    },
    handleChange() {
      console.log(this.addressSelect);
    },
    async showKuaiDi() {
      //物流进度请求数据
      const { data: res } = await this.$axios.get(`kuaidi/804909574412544580`);
      if (res.meta.status === 200) {
        this.kuaiDiList = res.data;
        this.lookDialogVisible = true;
      }
      console.log(this.kuaiDiList);
    }
  },
  created() {
    this.getGoodData();
  }
};
</script>

<style scoped>
.el-card {
  margin-top: 20px;
}
.el-table {
  margin-top: 20px;
}
</style>